//
//  XJTabBarController.m
//  AppFrame
//
//  Created by GXJ on 2017/2/24.
//  Copyright © 2017年 GXJ. All rights reserved.
//

#import "XJTabBarController.h"

#import "XJNavigationController.h"
#import "HomePageVC.h"
#import "ConvenientVC.h"
#import "WorkingVC.h"
#import "MemberCenterVC.h"
#import "XJTabBar.h"

@interface XJTabBarController ()<XJTabBarDelegate>
@property (nonatomic, weak)XJTabBar *xjTabBar;
@property(nonatomic,assign)NSInteger lastNo;
@end

@implementation XJTabBarController

-(void)viewWillLayoutSubviews{
    
    [super viewWillLayoutSubviews];
    self.tabBar.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.lastNo = 0;
    [self setupTabBar];
    [self setupAllChildViewControllers];
}

- (void)setupTabBar{

    //改变tabbar 线条颜色
    CGRect rect = CGRectMake(0, 0, KScreenWidth, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,[UIColor colorWithHexString:@"EDEDED"].CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setShadowImage:img];
    [self.tabBar setBackgroundImage:[[UIImage alloc]init]];
}

- (void)setupAllChildViewControllers
{
    HomePageVC *first = [[HomePageVC alloc] init];
    [self setupChildController:first title:@"首页" tabbarTitle:@"首页" imageName:@"tabbar_home_unselected" selectImageName:@"Combined Shape-selected"];
    
    ConvenientVC *second = [[ConvenientVC alloc] init];
    [self setupChildController:second title:@"便民服务" tabbarTitle:@"便民" imageName:@"便民" selectImageName:@"便民A"];
    
    WorkingVC *third = [[WorkingVC alloc] init];
    [self setupChildController:third title:@"办事" tabbarTitle:@"办事" imageName:@"办事" selectImageName:@"办事A"];
    
    MemberCenterVC *fourth = [[MemberCenterVC alloc] init];
    [self setupChildController:fourth title:@"我的" tabbarTitle:@"我的" imageName:@"我的" selectImageName:@"我的A"];
    
    
}

- (void)setupChildController:(UIViewController *)controller title:(NSString *)title tabbarTitle:(NSString *) tabbarTitle imageName:(NSString *)imageName selectImageName:(NSString *)selectImageName
{
    controller.title = title;
    controller.tabBarItem.image = [UIImage imageNamed:imageName];
    controller.tabBarItem.title = tabbarTitle;
    UIImage *selectedImage = [UIImage imageNamed:selectImageName];
    controller.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    XJNavigationController *navigationController = [[XJNavigationController alloc] initWithRootViewController:controller];
    navigationController.view.backgroundColor = [UIColor whiteColor];
    
    if ([title isEqualToString:@"我的"] || [title isEqualToString:@"首页"] || [title isEqualToString:@"办事"]) {
        controller.navigationController.navigationBar.hidden = YES;
    }
    
    [self addChildViewController:navigationController];
    
    [self.xjTabBar addTabBarItemWithItem:controller.tabBarItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
